<?php

namespace Account\Controller;

use Common\Controller\ApiController;

/**
 * 签到
 */
class SignController extends ApiController {

    public function getSignTable() {
        $user = $this->user;
        $time = time();
        $where['week'] = date('W', $time);
//        $where['week_day'] = date('week_day', $time);
        $where['user_id'] = $user['id'];
        $data = D('Sign')->where($where)->getField('week_day', true);
        for ($i = 1; $i <= 7; $i++) {
            if (in_array($i, $data)) {
                $week[] = 1;
            } else {
                $week[] = 0;
            }
        }
        $today = date('Y-m-d 00:00:00');
        $today = strtotime($today);
        $row = D('Sign')->where(['user_id' => $user['id'], 'type' => 1, 'add_time' => ['egt', $today]])->count();
        $type = $row ? $row : 0;
        $sign['week'] = $week;
        $sign['type'] = $type;
        $this->response($sign);
    }

    public function sign() {
        $user = $this->user;
        $input = $this->request();
        $time = time();
        $type = 0;

        $message = '签到成功';
        if (!empty($input['time'])) {//补签到
            $time = strtotime($input['time']);
            $type = 1; //补签
            $message = '补签成功';
        }

        $week = date("W", $time);
        $week_day = date("w", $time);
        if (D('Sign')->where(['user_id' => $user['id'], 'week' => $week, 'week_day' => $week_day])->select()) {
            $this->errorResponse('10001', '已签到过');
        }

        $data = [
            'user_id' => $user['id'],
            'signtime' => $time,
            'signtime2' => date("Y-m-d", $time),
            'week' => date("W", $time),
            'week_day' => date("w", $time),
            'type' => $type,
            'add_time' => time()
        ];

        if (D('Sign')->add($data)) {
            $GoldLogModel = D('Account/GoldLog');
            $gold = 30;
            if (date("w", $time) == 7) {
                $row = D('Sign')->where(['user' => $user['id'], 'week' => date("W", $time)])->count();
                if ($row == 7) {
                    $gold = 100;
                    $resp = ['gold' => $gold, 'message' => '连续七天签到成功'];
                } else {
                    $resp = ['gold' => $gold, 'message' => $message];
                }
            } else {
                $resp = ['gold' => $gold, 'message' => $message];
            }
            if ($type) $this->createdResponse($resp);
            $res2 = D('User')->where(['id' => $user['id']])->setInc('gold', $gold);
            $golddata = [
                'user_id' => $user['id'],
                'gold' => $gold,
                'mark' => '签到送书币',
                'type' => 'send',
                'add_time' => $time
            ];
            $result = $GoldLogModel->addGoldLog($golddata); //赠送记录
            if (!empty($res2) && !empty($result)) {
                $this->createdResponse($resp);
            }
        } else {
            $this->errorResponse('10001', '签到失败，请稍后再试');
        }
    }

}
